package com.lechuang.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.lechuang.common.context.ThreadLocalContext;
import com.lechuang.common.model.TokenBaseInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
@Slf4j
public class AdminMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        TokenBaseInfo tokenBaseInfo = ThreadLocalContext.baseInfoThreadLocal.get();
        this.setFieldValByName("createUserId", tokenBaseInfo.getAdminUserId(), metaObject);
        this.setFieldValByName("createUserName", tokenBaseInfo.getAdminUserName(), metaObject);
        this.setFieldValByName("updateUserId", tokenBaseInfo.getAdminUserId(), metaObject);
        this.setFieldValByName("updateUserName", tokenBaseInfo.getAdminUserName(), metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        TokenBaseInfo tokenBaseInfo = ThreadLocalContext.baseInfoThreadLocal.get();
        this.setFieldValByName("updateUserId", tokenBaseInfo.getAdminUserId(), metaObject);
        this.setFieldValByName("updateUserName", tokenBaseInfo.getAdminUserName(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}

